import { 战利品, ItemData, 手枪 } from "../Datas/Data";
import { Constant, ItemType } from "../Framework/Const/Constant";
import { BundleManager, Bundles } from "../Framework/Managers/BundleManager";
import { UIManager } from "../Framework/Managers/UIManager";
import NodeUtil from "../Framework/Utils/NodeUtil";
import { ResourceUtil } from "../Framework/Utils/ResourceUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GoodsItem extends cc.Component {

    Button: cc.Button = null;
    Icon: cc.Sprite = null;
    Name: cc.Label = null;
    Desc: cc.Label = null;
    Selected: cc.Node = null;
    Locked: cc.Node = null;
    Get: cc.Node = null;

    data: ItemData;

    cb: Function = null;

    protected onLoad(): void {
        this.Button = this.node.getComponent(cc.Button);
        this.Icon = NodeUtil.GetComponent("Icon", this.node, cc.Sprite);
        this.Name = NodeUtil.GetComponent("Name", this.node, cc.Label);
        this.Desc = NodeUtil.GetComponent("Desc", this.node, cc.Label);
        this.Selected = NodeUtil.GetNode("Selected", this.node);
        this.Locked = NodeUtil.GetNode("Locked", this.node);
        this.Get = NodeUtil.GetNode("Get", this.node);
    }

    Init(data: ItemData, cb: Function) {
        this.Reset();
        this.data = data;
        this.cb = cb;
        this.Name.string = `${this.data.Name}`;
        this.Desc.string = `${this.data.Count} `;

        let scale = 1;
        if (data.Type == ItemType[ItemType.头盔]) scale = 0.4;
        if (data.Type == ItemType[ItemType.防弹衣]) scale = 0.6;
        if (data.Type == ItemType[ItemType.手枪]) scale = 0.8;
        if (data.Type == ItemType[ItemType.突击步枪]) scale = 0.8;
        if (data.Type == ItemType[ItemType.冲锋枪]) scale = 0.8;
        if (data.Type == ItemType[ItemType.射手步枪]) scale = 0.8;
        if (data.Type == ItemType[ItemType.栓动步枪]) scale = 0.8;
        if (data.Type == ItemType[ItemType.轻机枪]) scale = 0.8;
        if (data.Type == ItemType[ItemType.霰弹枪]) scale = 0.8;
        if (data.Type == ItemType[ItemType.卡宾枪]) scale = 0.8;
        if (data.Type == ItemType[ItemType.子弹]) scale = 0.6;
        if (data.Type == ItemType[ItemType.战利品]) scale = 0.6;
        if (data.Type == ItemType[ItemType.药品]) scale = 0.6;
        if (data.Type == ItemType[ItemType.金钱]) scale = 0.5;

        this.Icon.node.setScale(scale);

        BundleManager.LoadSpriteFrame(Bundles.Item, `${data.Type}_${data.Name}`).then((sf: cc.SpriteFrame) => {
            this.Icon.spriteFrame = sf;
        });
    }

    InitBlank() {
        this.Reset();
        this.Button.enabled = false;
    }

    InitSimple(data: ItemData) {
        this.Reset();
        this.Button.enabled = false;
        BundleManager.LoadSpriteFrame(Bundles.Item, `${data.Type}_${data.Name}`).then((sf: cc.SpriteFrame) => {
            this.Icon.spriteFrame = sf;
        });
    }

    Reset() {
        this.data = null;
        this.cb = null;
        this.Button.enabled = true;
        this.Name.string = ``;
        this.Desc.string = ``;
        this.Icon.spriteFrame = null;
        this.Selected.active = false;
        this.Get.active = false;
        this.Locked.active = false;
    }

    SetSelected(data: ItemData) {
        this.Selected.active = data == this.data;
    }

    OnButtonClick() {
        if (this.Get.active) return;
        if (this.Locked.active) {
            UIManager.ShowTip("获得进阶手册可解锁更多奖励");
            return;
        }
        this.cb && this.cb(this.data);
    }

}
